package cn.deu.xunwu;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.dzu.dao.XunwuDao;
import cn.dzu.entity.Xunwu;

/**
 * Servlet implementation class addxunwu
 */
@WebServlet("/addxunwu")
public class addxunwu extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addxunwu() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		response.setContentType("text/html;charset=utf-8");
		//请求解决乱码
		request.setCharacterEncoding("utf-8");
		//响应解决乱码
		response.setCharacterEncoding("utf-8");
	
		String id1=request.getParameter("id");
		int id =Integer.parseInt(id1);	
		String  name=request.getParameter("sname");	
		String pass=request.getParameter("pass");	
		String	adress=request.getParameter("adress");	
		String dates=request.getParameter("date");				
		String	goods=request.getParameter("goods");
		java.util.Date date;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(dates);
			java.sql.Date  sqlDate  =  new java.sql.Date(date.getTime());  
			 Xunwu xun =new Xunwu(id, pass,sqlDate, adress, goods,name);
			   ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
			   XunwuDao xunwu=(XunwuDao)applicationContext.getBean("XunwuDao");	
			   int i=xunwu.add(xun);
			   if(i==1) {
				   request.getRequestDispatcher("/select").forward(request, response);
			   }else {
				   response.setContentType("text/html;charset=UTF-8");
				   PrintWriter out=response.getWriter();	
				   out.print("<script>alert('删除信息错误');</script>");
			   }
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}     
	}

}
